package database

import (
	log2 "chat/log"
	"chat/tools"
	"context"
	"errors"
	"fmt"
	"github.com/go-redis/redis/v8"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"strconv"
)

func ChatInTheGameBureau(ctx context.Context, roomId int, userAId int, userBId int) (int, error) {
	roomIdstr := strconv.Itoa(roomId) + strconv.Itoa(userAId) + strconv.Itoa(userBId)
	mysqlPath := tools.GetEnv("MYSQL_PATH", "localhost:3306")
	mysqlUser := tools.GetEnv("MYSQL_USER", "root")
	mysqlPassword := tools.GetEnv("MYSQL_PASSWORD", "123456")
	db, err := gorm.Open(mysql.Open(mysqlUser+":"+mysqlPassword+"@tcp("+mysqlPath+")/chats?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Error", "failed to connect database")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, errors.New("failed to connect database")
	}
	err = db.AutoMigrate(&tools.ChatRoomStruct{})
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, err
	}
	tmpChatRoom := tools.ChatRoomStruct{}
	roomId2, err := strconv.Atoi(roomIdstr)
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Error", "Data encoding format conversion error")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, err
	}
	log.Println(roomId2)
	db.Model(tools.ChatRoomStruct{}).Where("room_id=?", roomId2).First(&tmpChatRoom)
	log.Println(tmpChatRoom.RoomId)
	if tmpChatRoom.RoomId == 0 {
		//id, err2 := strconv.Atoi(strconv.Itoa(userAId) + strconv.Itoa(userBId))
		/*if err2 != nil {
			writeLog, err3 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Error", "Data encoding format conversion error")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err3 != nil {
				fmt.Println("写入日志失败")
			}
			return 0, err2
		}*/
		tmpChatRoom2 := tools.ChatRoomStruct{RoomId: roomId2, MasterId: userAId, IsPrivate: true}
		db.Model(tools.ChatRoomStruct{}).Create(&tmpChatRoom2)
		log.Println(1)
	}
	if tmpChatRoom.RoomId != 0 {
		/*id, err2 := strconv.Atoi(strconv.Itoa(userAId) + strconv.Itoa(userBId))
		if err2 != nil {
			writeLog, err3 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Error", "Data encoding format conversion error")
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err3 != nil {
				fmt.Println("写入日志失败")
			}
			return 0, err2
		}*/
		tmpChatRoom2 := tools.ChatRoomStruct{RoomId: roomId2, MasterId: userAId, IsPrivate: true}
		db.Model(tools.ChatRoomStruct{}).Where("room_id=?", roomId2).Updates(&tmpChatRoom2)
		log.Println(2)
	}
	log.Println(roomId2)
	db.Model(tools.ChatRoomStruct{}).Where("room_id=?", roomId2).First(&tmpChatRoom)
	if tmpChatRoom.RoomId == 0 {

		writeLog, err2 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Warring", "Failed to create chat room")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		return 0, errors.New("failed to create chat room")
	}
	redisPath := tools.GetEnv("REDIS_PATH", "localhost:6379")
	redisPD := tools.GetEnv("REDIS_PASSWORD", "")
	rdb := redis.NewClient(&redis.Options{
		Addr:     redisPath,
		Password: redisPD, // no password set
		DB:       2,       // use default DB
		//PoolSize: 500, // 连接池大小
	})
	_, err1 := rdb.SAdd(ctx, strconv.Itoa(roomId2), userAId).Result()
	if err1 != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Warring", err1.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, err1
	}
	_, err2 := rdb.SAdd(ctx, strconv.Itoa(roomId2), userBId).Result()
	if err2 != nil {
		writeLog, err3 := log2.WriteLog(ctx, "chat", "ChatInTheGameBureau", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Warring", err2.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, err2
	}

	result, err := rdb.SIsMember(ctx, strconv.Itoa(roomId2), userAId).Result()
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "CreateChatRoom", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, err
	}
	if result == false {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "CreateChatRoom", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Warring", "Creating the room was not completely successful")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, errors.New("the creation of a room is not completely successful")
	}
	result, err = rdb.SIsMember(ctx, strconv.Itoa(roomId2), userBId).Result()
	if err != nil {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "CreateChatRoom", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, err
	}
	if result == false {
		writeLog, err2 := log2.WriteLog(ctx, "chat", "CreateChatRoom", "userAId:"+strconv.Itoa(userAId)+"userBId:"+strconv.Itoa(userBId)+" roomid:"+strconv.Itoa(roomId), "Warring", "Creating the room was not completely successful")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return 0, errors.New("the creation of a room is not completely successful")
	}
	return roomId2, err
}
